<template>
  <div class="header">
    <div class="site-nav">
      <div class="site-nav-title">
        <img class="home-icon" src="@/assets/images/shouye.png" />
        <span @click="onToHomePageClick">欢迎来到耶购商城!</span>
        <span v-if="_userStore.userName" class="header-tab">{{ _userStore.userName }}</span>
        <span v-else class="header-tab" @click="onToLoginClick">登录</span>
      </div>
      <div class="site-nav-tab">
        <div class="header-tab" @click="router.push('/shoppingCart')">
          <img class="car-icon" src="@/assets/images/gouwuchexiao.png" />
          <span>采购单</span>
        </div>
        <span class="header-tab" @click="router.push('/allOrder')">我的订单</span>
        <span class="header-tab" @click="router.push('/user')">我的资料</span>
        <span class="header-tab" @click="onLogoutClick">退出</span>
      </div>
    </div>
  </div>
</template>
<script setup>
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { UserStore } from '@/store/user'
import api from '@/api'

const router = useRouter()
const _userStore = UserStore()

const toLogout = async () => {
  let { err } = await api.post('/medinstrument/site/customer/logout')
  if (err) {
    ElMessage.error(err)
  } else {
    router.push('/login')
    _userStore.logout()
  }
}

const onToHomePageClick = () => {
  let { href } = router.resolve({ path: '/home' })
  window.open(href, '_blank')
}

const onToLoginClick = () => {
  router.push('/login')
}

const onLogoutClick = () => {
  toLogout()
}
</script>

<style scoped>
.header {
  height: 27px;
  line-height: 27px;
  background-color: #f2f2f2;
}

.site-nav {
  width: 1080px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.home-icon {
  width: 14px;
  height: 12px;
  margin-right: 4px;
}
.car-icon {
  width: 14px;
  height: 14px;
  margin-right: 3px;
}
.site-nav-title,
.site-nav-tab {
  display: flex;
  align-items: center;
  font-size: 12px;
  color: #999;
  font-weight: 500;
}

.header-tab:hover {
  color: var(--color-primary);
  border-bottom: 1px solid var(--color-primary);
  cursor: pointer;
}

.site-nav-title span {
  cursor: pointer;
}

.site-nav-title span:last-child {
  margin-left: 10px;
  color: var(--color-primary);
}

.site-nav-tab :first-child {
  display: flex;
  align-items: center;
}

.site-nav-tab .header-tab {
  margin-left: 20px;
}
</style>
